package hottargui.config;

import java.util.*;

import hottargui.framework.PlayerColor;
import hottargui.framework.TurnSequenceStrategy;

public class GammaTurnSequenceStrategy implements TurnSequenceStrategy {

	List<PlayerColor> sequence;
	int idx;
	int playersLeft = 0;

	public GammaTurnSequenceStrategy() {
	}

	public PlayerColor nextPlayer() {
		PlayerColor next;
		if (playersLeft != 0) {
			next = sequence.get(idx % sequence.size());
			playersLeft--;
			idx++;
		} else {
			next = PlayerColor.None;
		}

		return next;
	}

	/**
	 * Initial value for playersLeftInTurn is undefined
	 */
	public int playersLeftInTurn() {
		return playersLeft;
	}

	public void startNewTurn(int diceValue) {
		sequence = new ArrayList<PlayerColor>();
		addPlayers(diceValue);
		java.util.Collections.shuffle(sequence); // Shuffle the player sequence
		doubleRoundsForPlayers(diceValue);
		playersLeft = sequence.size();
		System.out.println("Players left: " + playersLeft);
		idx = 0;
	}

	private void addPlayers(int playsPerPlayer) {
		if ((1 <= playsPerPlayer) && (playsPerPlayer <= 6)) // Precondition
		{
			int timesToAddPlayer = 0;
			if (playsPerPlayer == 6) {
				timesToAddPlayer = 1; // Handle the special case where the die value is 6
			} else {
				timesToAddPlayer = playsPerPlayer;
			}

			for (PlayerColor pc : PlayerColor.values()) {
				if (pc != PlayerColor.None) // None is not at player
				{
					for (int i = 0; i < timesToAddPlayer; i++) {
						sequence.add(pc);
					}
				}
			}
		}
	}

	public void doubleRoundsForPlayers(int playsPerPlayer) {
		if (playsPerPlayer == 6) {
			List<PlayerColor> tmpArr = new ArrayList<PlayerColor>();
			for (PlayerColor pc : sequence) {
				tmpArr.add(pc);
				tmpArr.add(pc);
			}
			sequence = tmpArr;
		}
	}

}
